package com.adtech.farmProducts.utils;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;

import com.adtech.common.utils.FileUtil;
import com.adtech.farmProducts.App;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Time:2019/10/10
 * Created by xiaocz
 * Describe
 */
public class ImageUtils {

    /**
     * 保存图片
     */
    public static void saveImage(String strFileName, View view, Context context) {

        if (getSDPath() == null) {
            App.getInstance().showMessage("SD卡不存在");
            return;
        }
        Bitmap bitmap = createViewBitmap(view);
        if (bitmap == null) {
            App.getInstance().showMessage("保存失败");
            return;
        }
        String strPath = getSDPath();

        try {
            File destDir = new File(strPath);
            if (!destDir.exists()) {
                Log.d("MagicMirror", "Dir not exist create it " + strPath);
                destDir.mkdirs();
                Log.d("MagicMirror", "Make dir success: " + strPath);
            }

            File imageFile = new File(strPath + "/" + strFileName);
            imageFile.createNewFile();
            FileOutputStream fos = new FileOutputStream(imageFile);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.flush();
            fos.close();
            App.getInstance().showMessage("保存成功，请到文件夹 /自己人代驾/Pictures/文件中查看");
            // 其次把文件插入到系统图库
            try {
                MediaStore.Images.Media.insertImage(context.getContentResolver(),
                        imageFile.getAbsolutePath(), strFileName, null);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            // 最后通知图库更新
            context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
                    Uri.fromFile(new File(imageFile.getPath()))));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            App.getInstance().showMessage("保存失败");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            App.getInstance().showMessage("保存失败");
            e.printStackTrace();
        }
    }

    private static String getSDPath() {
        boolean hasSDCard = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
        if (hasSDCard) {
            return FileUtil.createAppFoldersRootPath() + File.separator + "Pictures";
        }
        return null;
    }

    /**
     * 画布方式转换
     *
     * @param v
     * @return
     */
    private static Bitmap createViewBitmap(View v) {
        Bitmap bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        v.draw(canvas);
        return bitmap;
    }
}
